
function checkConfig(dataId,a,id,fd,nbRouterUp,interfaces)
{
	
	var interfaceToCheck,ipAddressToCheck,maskToCheck,interfaceFromCheck,ipAddressFromCheck,maskFromCheck,nodeIdTo,routerIdTo,nodeIdFrom,routerIdFrom;
	a.data.ospfstate = "down";
	
	var returnArray = ["error","",0,""];
	
	var inputInterface = document.getElementById("interface"+dataId).value;
	var inputAddress = document.getElementById("addressip"+dataId).value;
	var inputMask = document.getElementById("mask"+dataId).value;
	
	
	inputInterface = escapeRegExp(inputInterface);
	inputAddress = escapeRegExp(inputAddress);
	inputMask = escapeRegExp(inputMask);
	
	for (i = 0; i < interfaces.length ; i++) {
		if(inputInterface == interfaces[i]) {
			returnArray[1] = " You can not connect more than one network on an interface. Please check interface " + inputInterface + ".<br>";		
			return returnArray;
		}
	}
	
	if(a.data.type == "switch-router"){
		ipAddressToCheck = inputAddress;
		maskToCheck = inputMask;
		
		a.data.nodeFromIpAddress = ipAddressToCheck;
		a.data.nodeFromMask = maskToCheck;
		a.data.nodeToIpAddress = ipAddressToCheck;
		a.data.nodeToMask =  maskToCheck;
		
		// nodeIdTo = a.data.nodeFrom;
		nodeIdTo = a.data.nodeTo;
		
		var nodeToAdj = fd.graph.nodes[nodeIdTo].adjacencies;
		var networkAddress =  getNetWorkAddress(ipAddressToCheck,maskToCheck);
		
		var ackMessage = "";
		var hostList = [];
		for(var id1 in nodeToAdj) {
			var a1 = nodeToAdj[id1]; 
			var adjtype = a1.data.type;
			
			if(adjtype == "host-switch"){
				ipAddressHost = a1.data.nodeFromIpAddress;
				
				// equalize ip address from and to
				a1.data.nodeToIpAddress = egalize(a1.data.nodeToIpAddress,a1.data.nodeFromIpAddress);
				a1.data.nodeFromIpAddress = a1.data.nodeToIpAddress;
				// equalize mask from and to
				a1.data.nodeToMask = egalize(a1.data.nodeToMask,a1.data.nodeFromMask);
				a1.data.nodeFromMask = a1.data.nodeToMask;
				
				// verify mask
				maskFromCheck = a1.data.nodeFromMask;
				if (maskToCheck != maskFromCheck){
					returnArray[1] = "Network " + networkAddress + ": More than one host mask is not well defined.<br>";
					return returnArray;
				}
				// verify network
				if( networkAddress != getNetWorkAddress(a1.data.nodeToIpAddress,a1.data.nodeToMask)){
					returnArray[1] = "Network " + networkAddress + ": More than one host ip address is not well defined.<br>";
					return returnArray;
				}
				//same address than network :
				if(networkAddress == a1.data.nodeToIpAddress){
					returnArray[1] = "Network " + networkAddress + ": One or more host have the same ip address than the network address.<br>";
					return returnArray;
				}
				//same address than broadcast :
				var broadcastAddress = getBroadcastAddress(networkAddress,maskToCheck);
				if(broadcastAddress == a1.data.nodeToIpAddress){
					returnArray[1] = "Network " + networkAddress + ": One or more host have the same ip address than the broadcast address.<br>";
					return returnArray;
				}
				// verify ip address
				for(var host = 0; host< hostList.length; host++){
					if(hostList[host] == a1.data.nodeToIpAddress){
						returnArray[1] = "Network " + networkAddress + ": Two or more hosts have the same ip address.<br>";
						return returnArray;
					}
				}				
				hostList.push(a1.data.nodeFromIpAddress);
			}
		}
		
		if(hostList.length == 0){
			ackMessage = "Network " + networkAddress + ": No host connected. ";
			maskFromCheck = maskToCheck;
		}
		else {
			ackMessage = "Network " + networkAddress + ": All hosts are perfectly defined. ";
		}
		
		
		//if state is up ?
		var stateValue = document.getElementById("ospfstate"+dataId).innerHTML;
		
		if(stateValue == "up")
		{
			a.data.ospfstate = "up";
			ackMessage += " <strong>Interface is up.</strong> ";
			
			var ospfcostnetwork = calculCost(a.data.bandwidth);
			var maskprefix = getmaskprefix(maskFromCheck);
			
			returnArray[3] = '<direct id="' + nbRouterUp + '" network="' + networkAddress + '" cost="' + ospfcostnetwork + '" nexthop="' + ipAddressFromCheck + '" nodeid="" mask="' + maskprefix + '" depth="0" ></direct>';
			// routerIdTo = routerIdFrom // nodeIdFrom = nodeIdTo
			returnArray[2] = 1; // +1 router up
		}
		else
		{
			ackMessage += " <strong>Interface is down.</strong> ";
		}
		
		ackMessage += "<br>";
		returnArray[0] = 'acknow';
		returnArray[1] = ackMessage;
		return returnArray;
	}
	else if(a.data.type == "router-router"){
		if(id == a.data.nodeTo){ // if nodeFrom is init node 
			interfaceToCheck = inputInterface;
			ipAddressToCheck = inputAddress;
			maskToCheck = inputMask;

			interfaceFromCheck = a.data.nodeToInterface;
			ipAddressFromCheck = a.data.nodeToIpAddress;
			maskFromCheck = a.data.nodeToMask;
					
			// vérifier que a.data.nodeFromInterface est bien actualisée
			a.data.nodeFromInterface = interfaceToCheck;
			a.data.nodeFromIpAddress = ipAddressToCheck;
			a.data.nodeFromMask = maskToCheck;
			
			var test1 = a.nodeFrom.id;
			var test2 = a.nodeTo.id;
			
			nodeIdTo = a.data.nodeTo;
			routerIdFrom = fd.graph.nodes[nodeIdTo].data.ospfRouterId;
			
			nodeIdFrom = a.data.nodeFrom;
			routerIdTo = fd.graph.nodes[nodeIdFrom].data.ospfRouterId; 		
			
			if(null == interfaceToCheck || null == interfaceFromCheck )
			{
				returnArray[1] = " Interfaces undefined ! <br>";
				return returnArray;
			}		
			
			if(interfaceToCheck.substring(0,interfaceToCheck.length - 1) != interfaceFromCheck.substring(0,interfaceFromCheck.length - 1)) {
				returnArray[1] = " Interfaces do not have the same type. Please check interface " + interfaceFromCheck + "and " + interfaceToCheck + ".<br>";		
				return returnArray;
			}
		}
		else if (id == a.data.nodeFrom) { // if nodeFrom is destination node 
			interfaceToCheck = inputInterface;
			ipAddressToCheck = inputAddress;
			maskToCheck = inputMask;
			
			interfaceFromCheck = a.data.nodeFromInterface;
			ipAddressFromCheck = a.data.nodeFromIpAddress;
			maskFromCheck = a.data.nodeFromMask;
			
			// vérifier que a.data.nodeFromInterface est bien actualisée
			a.data.nodeToInterface = inputInterface;
			a.data.nodeToIpAddress = inputAddress;
			a.data.nodeToMask = inputMask;
			
			nodeIdTo = a.data.nodeFrom;
			routerIdTo = fd.graph.nodes[nodeIdTo].data.ospfRouterId;
			
			nodeIdFrom = a.data.nodeTo;
			routerIdFrom = fd.graph.nodes[nodeIdFrom].data.ospfRouterId; 			
			
			if(null == interfaceToCheck || null == interfaceFromCheck )
			{
				returnArray[1] = " Interfaces undefined ! <br>";
				return returnArray;
			}
			if(interfaceToCheck.substring(0,interfaceToCheck.length - 1) != interfaceFromCheck.substring(0,interfaceFromCheck.length - 1)) {
				returnArray[1] = " Interfaces do not have the same type. Please check interface " + interfaceFromCheck + "and " + interfaceToCheck + ".<br>";
				return returnArray;
			}
		}
		else {throw "error";}
	}
	
	// interfaces have the same Ip Address
	if(ipAddressToCheck == ipAddressFromCheck)
	{
		returnArray[1] = " Same Ip Interface : "  + ipAddressToCheck + " == " +ipAddressFromCheck+ " .<br>";		
		return returnArray;
	}
	// submasks do not match
	if(maskFromCheck != maskToCheck)
	{
		returnArray[1] = " Bad Mask : "  + maskToCheck + " != " + maskFromCheck+ " .<br>";		
		return returnArray;
	}	
	// Address or submask are not defined properly
	var networkAddressTo = getNetWorkAddress(ipAddressToCheck,maskToCheck);
	var networkAddressFrom = getNetWorkAddress(ipAddressFromCheck,maskFromCheck);
	if(networkAddressTo == "error" || networkAddressFrom == "error")
	{
		returnArray[1] = "Address or mask error : Ip Interface " + ipAddressToCheck + " mask : " + maskToCheck + " and  Ip Interface " + ipAddressFromCheck + " mask : " + maskFromCheck + " .<br>";		
		return returnArray;
	}
	// One or two masks are not defined properly
	if (!checkMask(maskToCheck) || !checkMask(maskFromCheck))
	{
		returnArray[1] = "Mask error : Ip Interface " + ipAddressToCheck + " mask : " + maskToCheck + " and  Ip Interface " + ipAddressFromCheck + " mask : " + maskFromCheck + " .<br>";
		return returnArray;
	}
	if(networkAddressTo == ipAddressToCheck || networkAddressFrom == ipAddressFromCheck)
	{
		returnArray[1] = " Interfaces can not have the same Ip than their Network.  Please check it out : Ip Interface " + ipAddressToCheck + " mask : " + maskToCheck + " and  Ip Interface " + ipAddressFromCheck + " mask : " + maskFromCheck + " .<br>";
		return returnArray;
	}
	// networks do not match
	if(networkAddressTo != networkAddressFrom)
	{
		returnArray[1] = "Interface with ip address " + ipAddressToCheck + " Mask:"+ maskToCheck + " is supposed to have the same network that the connecting interface with ip address " + ipAddressFromCheck + " Mask:"+ maskFromCheck + ". Please make sure that interfaces and their netmasks are configured properly. <br>";
		return returnArray;
	}
	// one or two of the interface Ip have the same address than the broadcast address
	var broadcastAddress = getBroadcastAddress(networkAddressTo,maskToCheck);
	if(broadcastAddress == ipAddressToCheck || broadcastAddress == ipAddressFromCheck)
	{
		returnArray[1] = " Interfaces can not have the same Ip than their Broadcast.  Please check it out : Ip Interface " + ipAddressToCheck + " mask : " + maskToCheck + " and  Ip Interface " + ipAddressFromCheck + " mask : " + maskFromCheck + " .<br>";
		return returnArray;
	}

	var ackMessage = " OSPF protocol is ready to start between Interface " + ipAddressToCheck +  " and interface" + ipAddressFromCheck + " .";
	
	//if state is up ?
	var stateValue = document.getElementById("ospfstate"+dataId).innerHTML;
	
	if(stateValue == "up")
	{
		a.data.ospfstate = "up";
		ackMessage += " <strong>Interface is up.</strong> ";
		
		var ospfcostnetwork = calculCost(a.data.bandwidth);
		var maskprefix = getmaskprefix(maskFromCheck);
		
		returnArray[3] = '<direct id="' + nbRouterUp + '" network="' + networkAddressTo + '" cost="' + ospfcostnetwork + '" nexthop="' + ipAddressFromCheck + '" nodeid="' + nodeIdTo + '" mask="' + maskprefix + '" depth="0" ></direct>';
		// routerIdTo = routerIdFrom // nodeIdFrom = nodeIdTo
		returnArray[2] = 1; // +1 router up
	}
	else
	{
		ackMessage += " <strong>Interface is down.</strong> ";
	}
	ackMessage += "<br>";
	
	returnArray[0] = 'acknow';
	returnArray[1] = ackMessage; 
	
	
	return returnArray;
}


function escapeRegExp(string) {
    return string.replace(/([*-+?^=!:${}()|\[\]\/\\])/g, '');
}

function replaceAll(string, find, replace) {
  return string.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}